<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/2/1
 * Time: 14:25
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Goods extends Base
{
    /**
     * 产品首页
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $goods_name = input('goods_name');
            $goods_f_cate = input('f_cate');
            $goods_s_cate = input('s_cate');
            $goods_t_cate = input('t_cate');
            $supplier_id = input('supplier_id');
            $goods_status = input('goods_status');
            $goods_recommend = input('goods_recommend');
            $goods_cu = input('goods_cu');
            $where['goods_status'] = ['BETWEEN', [1, 2]];

            if($goods_name) $where['goods_name_en|goods_name_es|goods_name_tp'] = ['LIKE', '%' . $goods_name . '%'];
            if($goods_f_cate) $where['goods_f_cate'] = $goods_f_cate;
            if($goods_s_cate) $where['goods_s_cate'] = $goods_s_cate;
            if($goods_t_cate) $where['goods_t_cate'] = $goods_t_cate;
            if($supplier_id) $where['goods_supplier_id'] = $supplier_id;
            if($goods_status) $where['goods_status'] = $goods_status;
            if($goods_recommend) $where['goods_recommend'] = $goods_recommend;
            if($goods_cu) $where['goods_cu'] = $goods_cu;

            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_goods')
                ->join('up_goods_cate gc1', 'gc1.cate_id = goods_f_cate', 'LEFT')
                ->join('up_goods_cate gc2', 'gc2.cate_id = goods_s_cate', 'LEFT')
                ->join('up_goods_cate gc3', 'gc3.cate_id = goods_t_cate', 'LEFT')
                ->join('up_supplier', 'supplier_id = goods_supplier_id', 'LEFT')
                ->join('up_goods_attr', 'attr_id = goods_attr', 'LEFT')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_goods')
                ->join('up_goods_cate gc1', 'gc1.cate_id = goods_f_cate', 'LEFT')
                ->join('up_goods_cate gc2', 'gc2.cate_id = goods_s_cate', 'LEFT')
                ->join('up_goods_cate gc3', 'gc3.cate_id = goods_t_cate', 'LEFT')
                ->join('up_supplier', 'supplier_id = goods_supplier_id', 'LEFT')
                ->join('up_goods_attr', 'attr_id = goods_attr', 'LEFT')
                ->where($where)
                ->field([
                    'goods_id',
                    'goods_name_en',
                    'goods_name_es',
                    'goods_name_tp',
                    'goods_thumb',
                    'goods_price',
                    'goods_cu_price',
                    'supplier_name',
                    'goods_status',
                    'gc1.cate_name_en f_cate_name',
                    'gc2.cate_name_en s_cate_name',
                    'gc3.cate_name_en t_cate_name',
                    'goods_recommend',
                    'goods_cu',
                    'goods_time',
                ])
                ->page($Nowpage, $limits)
                ->order('goods_time desc')
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $cate = Db::table('up_goods_cate')
            ->where('cate_level', 1)
            ->where('cate_status', 1)
            ->field('cate_id, cate_name_en')
            ->select();
        $supplier = Db::table('up_supplier')->where('supplier_status', 1)->field('supplier_id, supplier_name')->select();
        $this->assign([
            'cate' => $cate,
            'supplier' => $supplier
        ]);
        return $this->fetch();
    }

    /**
     * 新增产品
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function goodsAdd(){
        if(Request::instance()->isPost()){
            $goods_name_en = input('name_en');
            $goods_name_es = input('name_es');
            $goods_name_tp = input('name_tp');
            $goods_thumb = input('goods_thumb');
            $goods_price = input('goods_price');
            $goods_cu_price = input('goods_cu_price');
            $f_cate = input('f_cate');
            $s_cate = input('s_cate') ?: 0;
            $t_cate = input('t_cate') ?: 0;
            $supplier_id = input('supplier_id');
            $goods_attr = input('goods_attr');
            $goods_packaging = input('goods_packaging');
            $goods_images = input('goods_images');
            $goods_status = input('goods_status');
            $goods_recommend = input('goods_recommend');
            $goods_cu = input('goods_cu');
            $spec_en = input('spec_en');
            $spec_es = input('spec_es');
            $spec_tp = input('spec_tp');
            $spec_value = input('spec_value/a');
            $spec_price = input('spec_price/a');
            $spec_cu_value = input('spec_cu_value/a');
            $intro_en = input('intro_en');
            $intro_es = input('intro_es');
            $intro_tp = input('intro_tp');
            $detail_en = input('detail_en');
            $detail_es = input('detail_es');
            $detail_tp = input('detail_tp');

            if(!$goods_packaging) return json(['code' => 100, 'msg' => '请选择包装']);

            $insert = [
                'goods_name_en' => $goods_name_en,
                'goods_name_es' => $goods_name_es,
                'goods_name_tp' => $goods_name_tp,
                'goods_thumb' => $goods_thumb,
                'goods_price' => $goods_price,
                'goods_cu_price' => $goods_cu_price,
                'goods_images' => $goods_images,
                'goods_spec_name_en' => $spec_en,
                'goods_spec_name_es' => $spec_es,
                'goods_spec_name_tp' => $spec_tp,
                'goods_supplier_id' => $supplier_id,
                'goods_status' => $goods_status,
                'goods_f_cate' => $f_cate,
                'goods_s_cate' => $s_cate,
                'goods_t_cate' => $t_cate,
                'goods_attr' => $goods_attr,
                'goods_packaging' => $goods_packaging,
                'goods_recommend' => $goods_recommend,
                'goods_cu' => $goods_cu,
                'goods_intro_en' => $intro_en,
                'goods_intro_es' => $intro_es,
                'goods_intro_tp' => $intro_tp,
                'goods_detail_en' => $detail_en,
                'goods_detail_es' => $detail_es,
                'goods_detail_tp' => $detail_tp,
                'goods_time' => initTime()
            ];

            $specAll = [];
            if(is_array($spec_value) && is_array($spec_price) && is_array($spec_cu_value)){
                foreach ($spec_value as $k => $v){
                    $specAll[$k] = [
                        'spec_value' => $v,
                        'spec_price' => $spec_price[$k],
                        'spec_cu_price' => $spec_cu_value[$k],
                        'spec_time' => initTime()
                    ];
                }
            }else{
                return json(['code' => 100, 'msg' => '规格值参数错误']);
            }

            Db::startTrans();
            try{
                // 产品表
                $goods_id = Db::table('up_goods')->insertGetId($insert);

                // 规格
                if($specAll){
                    foreach ($specAll as &$va){
                        $va['spec_goods_id'] = $goods_id;
                    }
                    Db::table('up_goods_spec')->insertAll($specAll);
                }

                Db::commit();
                return json(['code' => 200, 'msg' => '添加成功']);
            }catch (\Exception $e){
                Db::rollback();
                return json(['code' => 100, 'msg' => $e->getMessage()]);
            }

        }
        $cate = Db::table('up_goods_cate')
            ->where('cate_level', 1)
            ->where('cate_status', 1)
            ->field('cate_id, cate_name_en')
            ->order('cate_sort desc')
            ->select();
        $supplier = Db::table('up_supplier')->where('supplier_status', 1)->field('supplier_id, supplier_name')->select();
        $attr = Db::table('up_goods_attr')
            ->where('attr_status', 1)
            ->field('attr_id, attr_name_en')
            ->order('attr_sort desc')
            ->select();
        $packaging = Db::table('up_goods_packaging')
            ->where('packaging_status', 1)
            ->field('packaging_id, packaging_name')
            ->order('packaging_sort desc')
            ->select();
        $this->assign([
            'cate' => $cate,
            'supplier' => $supplier,
            'attr' => $attr,
            'packaging' => $packaging
        ]);
        return $this->fetch();
    }

    /**
     * 产品详情
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function goodsDetail(){
        $goods_id = input('goods_id');
        $goods = Db::table('up_goods')
            ->join('up_goods_cate gc1', 'gc1.cate_id = goods_f_cate', 'LEFT')
            ->join('up_goods_cate gc2', 'gc2.cate_id = goods_s_cate', 'LEFT')
            ->join('up_goods_cate gc3', 'gc3.cate_id = goods_t_cate', 'LEFT')
            ->join('up_supplier', 'supplier_id = goods_supplier_id', 'LEFT')
            ->join('up_goods_attr', 'attr_id = goods_attr', 'LEFT')
            ->where('goods_id', $goods_id)
            ->field([
                'goods_name_en',
                'goods_name_es',
                'goods_name_tp',
                'goods_thumb',
                'goods_price',
                'goods_cu_price',
                'goods_images',
                'goods_spec_name_en',
                'goods_spec_name_es',
                'goods_spec_name_tp',
                'supplier_name',
                'goods_status',
                'gc1.cate_name_en f_cateName',
                'gc2.cate_name_en s_cateName',
                'gc3.cate_name_en t_cateName',
                'attr_name_en',
                'goods_packaging',
                'goods_recommend',
                'goods_cu',
                'goods_intro_en',
                'goods_intro_es',
                'goods_intro_tp',
                'goods_detail_en',
                'goods_detail_es',
                'goods_detail_tp'
            ])
            ->find();

        if($goods['goods_images']){
            $goods['goods_images'] = explode(',', $goods['goods_images']);
        }else{
            $goods['goods_images'] = [];
        }

        // 包装
        $packaging = Db::table('up_goods_packaging')->where('packaging_id', 'IN', $goods['goods_packaging'])->column('packaging_name');
        $goods['goods_packaging'] = implode(',', $packaging);

        // 规格
        $spec = Db::table('up_goods_spec')
            ->where('spec_goods_id', $goods_id)
            ->where('spec_del', 1)
            ->field('spec_value, spec_price, spec_cu_price')->select();

        $this->assign([
            'goods' => $goods,
            'spec' => $spec
        ]);
        return $this->fetch();
    }

    public function goodsEdit(){
        $goods_id = input('goods_id');
        if(Request::instance()->isPost()){
            $spec_change = input('spec_change');
            $goods_name_en = input('name_en');
            $goods_name_es = input('name_es');
            $goods_name_tp = input('name_tp');
            $goods_thumb = input('goods_thumb');
            $goods_price = input('goods_price');
            $goods_cu_price = input('goods_cu_price');
            $f_cate = input('f_cate');
            $s_cate = input('s_cate') ?: 0;
            $t_cate = input('t_cate') ?: 0;
            $supplier_id = input('supplier_id');
            $goods_attr = input('goods_attr');
            $goods_packaging = input('goods_packaging');
            $goods_images = input('goods_images');
            $goods_status = input('goods_status');
            $goods_recommend = input('goods_recommend');
            $goods_cu = input('goods_cu');
            $spec_en = input('spec_en');
            $spec_es = input('spec_es');
            $spec_tp = input('spec_tp');
            $spec_value = input('spec_value/a');
            $spec_price = input('spec_price/a');
            $spec_cu_value = input('spec_cu_value/a');
            $intro_en = input('intro_en');
            $intro_es = input('intro_es');
            $intro_tp = input('intro_tp');
            $detail_en = input('detail_en');
            $detail_es = input('detail_es');
            $detail_tp = input('detail_tp');

            if(!$goods_packaging) return json(['code' => 100, 'msg' => '请选择包装']);

            $insert = [
                'goods_name_en' => $goods_name_en,
                'goods_name_es' => $goods_name_es,
                'goods_name_tp' => $goods_name_tp,
                'goods_thumb' => $goods_thumb,
                'goods_price' => $goods_price,
                'goods_cu_price' => $goods_cu_price,
                'goods_images' => $goods_images,
                'goods_spec_name_en' => $spec_en,
                'goods_spec_name_es' => $spec_es,
                'goods_spec_name_tp' => $spec_tp,
                'goods_supplier_id' => $supplier_id,
                'goods_status' => $goods_status,
                'goods_f_cate' => $f_cate,
                'goods_s_cate' => $s_cate,
                'goods_t_cate' => $t_cate,
                'goods_attr' => $goods_attr,
                'goods_packaging' => $goods_packaging,
                'goods_recommend' => $goods_recommend,
                'goods_cu' => $goods_cu,
                'goods_intro_en' => $intro_en,
                'goods_intro_es' => $intro_es,
                'goods_intro_tp' => $intro_tp,
                'goods_detail_en' => $detail_en,
                'goods_detail_es' => $detail_es,
                'goods_detail_tp' => $detail_tp,
                'goods_update_time' => initTime()
            ];

            $specAll = [];
            if($spec_change == 2){
                if(is_array($spec_value) && is_array($spec_price) && is_array($spec_cu_value)){
                    foreach ($spec_value as $k => $v){
                        $specAll[$k] = [
                            'spec_goods_id' => $goods_id,
                            'spec_value' => $v,
                            'spec_price' => $spec_price[$k],
                            'spec_cu_price' => $spec_cu_value[$k],
                            'spec_time' => initTime()
                        ];
                    }
                }else{
                    return json(['code' => 100, 'msg' => '规格值参数错误']);
                }
            }

            Db::startTrans();
            try{
                // 产品表
                Db::table('up_goods')->where('goods_id',$goods_id)->update($insert);
                // 规格
                if($specAll){
                    Db::table('up_goods_spec')->where('spec_goods_id',$goods_id)->setField('spec_del',2);
                    Db::table('up_goods_spec')->insertAll($specAll);
                }

                Db::commit();
                return json(['code' => 200, 'msg' => '操作成功']);
            }catch (\Exception $e){
                Db::rollback();
                return json(['code' => 100, 'msg' => $e->getMessage()]);
            }

        }
        $goods = Db::table('up_goods')
            ->where('goods_id', $goods_id)
            ->find();
        $pack_arr = [];
        if($goods['goods_packaging']) $pack_arr = explode(',', $goods['goods_packaging']);
        $cate = Db::table('up_goods_cate')
            ->where('cate_level', 1)
            ->where('cate_status', 1)
            ->field('cate_id, cate_name_en')
            ->order('cate_sort desc')
            ->select();
        $cate2 = Db::table('up_goods_cate')
            ->where('cate_level', 2)
            ->where('cate_status', 1)
            ->where('cate_pid', $goods['goods_f_cate'])
            ->field('cate_id, cate_name_en')
            ->order('cate_sort desc')
            ->select();
        $cate3 = [];
        if($goods['goods_s_cate']){
            $cate3 = Db::table('up_goods_cate')
                ->where('cate_level', 3)
                ->where('cate_status', 1)
                ->where('cate_pid', $goods['goods_s_cate'])
                ->field('cate_id, cate_name_en')
                ->order('cate_sort desc')
                ->select();
        }
        $supplier = Db::table('up_supplier')->where('supplier_status', 1)->field('supplier_id, supplier_name')->select();
        $attr = Db::table('up_goods_attr')
            ->where('attr_status', 1)
            ->field('attr_id, attr_name_en')
            ->order('attr_sort desc')
            ->select();
        $packaging = Db::table('up_goods_packaging')
            ->where('packaging_status', 1)
            ->field('packaging_id, packaging_name')
            ->order('packaging_sort desc')
            ->select();
        $specs = Db::table('up_goods_spec')
            ->where('spec_del', 1)
            ->where('spec_goods_id', $goods['goods_id'])
            ->select();
        $this->assign([
            'data' => $goods,
            'specs' => $specs,
            'pack_arr' => $pack_arr,
            'cate' => $cate,
            'cate2' => $cate2,
            'cate3' => $cate3,
            'supplier' => $supplier,
            'attr' => $attr,
            'packaging' => $packaging
        ]);
        return $this->fetch();
    }

    /**
     * 上架/下架
     * @return \think\response\Json
     */
    public function goodsChangeStatus(){
        $goods_id = input('id');
        $status = Db::table('up_goods')->where('goods_id', $goods_id)->value('goods_status');
        $newStatus = $status == 1 ? 2 : 1;
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_status', $newStatus);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 推荐/未推荐
     * @return \think\response\Json
     */
    public function goodsChangeRecommend(){
        $goods_id = input('id');
        $recommend = Db::table('up_goods')->where('goods_id', $goods_id)->value('goods_recommend');
        $newRecommend = $recommend == 1 ? 2 : 1;
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_recommend', $newRecommend);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 促销/未促销
     * @return \think\response\Json
     */
    public function goodsChangeCu(){
        $goods_id = input('id');
        $cu = Db::table('up_goods')->where('goods_id', $goods_id)->value('goods_cu');
        $newCu = $cu == 1 ? 2 : 1;
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_cu', $newCu);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function goodsDel(){
        $goods_id = input('id');
        $res = Db::table('up_goods')->where('goods_id', $goods_id)->setField('goods_status', 3);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}